<?php
/**
 * Created by PhpStorm.
 * User: Hong
 * Date: 2018/3/19
 * Time: 11:05
 * Function:
 */

namespace Admin\Controllers\Organizes;
use Admin\Controllers\BaseAdminController;
use Illuminate\Http\Request;
use Platform\Models\Organize\LeaderMenu;
use Platform\Models\Organize\LeaderPermission;
use Platform\System\Tools\ResultTool;


/**
 *  组织菜单管理
 * Class MenuController
 * @package App\Http\Controllers\Admin\System
 */
class OrganizeMenuController extends BaseAdminController
{
    /**
     *   菜单列表
     */
    public function index()
    {
        return myResult()->setData((new LeaderMenu())->toTree())->send();
    }

    /**
     *   创建菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $data = $this->verify($request);

        $menu = LeaderMenu::create($data);

        if ($menu->id == $menu->parent_id) {
            $menu->delete();
            myResult()->stop(ResultTool::CODE_TARGET_WRONG,'父级id出错');
        }

        if ($menu && !empty($data['permission_id'])) {
            $menu->permission_uri = $menu->permission->uri;
            $menu->save();
        }

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'创建成功')->send();
    }

    /**
     *   菜单详情
     *
     * @param LeaderMenu $menu
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(LeaderMenu $menu)
    {
        $menu = $menu->filterNested($menu->toArray());
        return myResult()->setData($menu)->send();
    }

    /**
     *   修改菜单
     *
     * @param Request $request
     * @param LeaderMenu $menu
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, LeaderMenu $menu)
    {
        $data = $this->verify($request);
        $menu->update($data);

        if (!$menu->permission_id) {
            $menu->permission_uri = null;
        } else {
            $menu->permission_uri = $menu->permission->uri;
        }
        $menu->save();
        return myResult()->setCode(ResultTool::CODE_SUCCESS,'修改成功')->send();

    }

    /**
     *   删除菜单
     *
     * @param LeaderMenu $menu
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(LeaderMenu $menu)
    {
        $menu->delete();

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'删除成功')->send();
    }

    /**
     *   调整菜单顺序
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function order()
    {
        if (request()->ajax()) {
            $_order = request()->get('_order');
            $order = json_decode($_order, true);

            if (json_last_error() != JSON_ERROR_NONE) {
                throw new \InvalidArgumentException(json_last_error_msg());
            }

            LeaderMenu::saveOrder($order);
            return myResult()->setCode(ResultTool::CODE_SUCCESS,'操作成功')->send();
        }

        return myResult()->setCode(ResultTool::CODE_TARGET_WRONG,'操作失败')->send();
    }

    /**
     * 数据验证
     * @param Request $request
     * @return array
     */
    public function verify(Request $request){

        $data = $request->all(['parent_id','vue_path','vue_component','vue_title', 'vue_name', 'vue_icon', 'permission_id']);

        $data['parent_id'] = (int) $data['parent_id'];

        if (empty($data['vue_path']))
            myResult()->stop(ResultTool::CODE_PARAM_LOST,'vue_path不可为空');

        if (empty($data['vue_component']))
            myResult()->stop(ResultTool::CODE_PARAM_LOST,'vue_component不可为空');

        if (empty($data['vue_title']))
            myResult()->stop(ResultTool::CODE_PARAM_LOST,'vue_title不可为空');

        if ($data['parent_id'] ){
            $menu_log = LeaderMenu::find($data['parent_id']);
            if (!$menu_log) {
                myResult()->stop(ResultTool::CODE_TARGET_WRONG,'父级菜单不存在');
            }
        }
        if (!empty($data['permission_id'])){
            $permission_log = LeaderPermission::find($data['permission_id']);
            if (!$permission_log) {
                unset($data['permission_id']);
//                myResult()->stop(ResultTool::CODE_TARGET_WRONG,'权限不存在');
            }
        }

        $res_data = [
            'parent_id' => $data['parent_id'],
            'info' => [
                'path' => $data['vue_path'],
                'component' => $data['vue_component'],
                'title' => $data['vue_title'],
                'name' => $data['vue_name'],
                'icon' => $data['vue_icon'],
            ],
            'permission_id' => isset($data['permission_id']) ? $data['permission_id'] : null,
        ];

        return $res_data;
    }
}